package com.hz.us.controller;

import com.hz.api.model.request.ApiResponse;
import com.hz.us.service.UserManagerService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RequestMapping("/user")
@RestController
public class UserLoginController {

    @Resource
    UserManagerService userManagerService;

    @PostMapping("/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        @RequestParam("userType") Integer userType) {

        String result = userManagerService.login(username, password, userType);
        return result != null ? ApiResponse.ok(result) : ApiResponse.intError();
    }

    @PostMapping("/logout")
    public String logout(@RequestParam("username") String username) {
        int result = userManagerService.logout(username);
        return result > 0 ? ApiResponse.ok(result) : ApiResponse.intError();
    }

    @PostMapping("/change-pwd")
    public String changePwd(@RequestParam("userId") Integer userId, @RequestParam("originPwd") String originPwd,
                            @RequestParam("newPwd") String newPwd,  @RequestParam("newPwd2") String newPwd2) {
        if (!newPwd.equals(newPwd2)) {
            return ApiResponse.intError();
        }
        int change = userManagerService.changePwd(userId, originPwd, newPwd);
        return change > 0 ? ApiResponse.ok(change) : ApiResponse.intError();
    }

}
